#include<iostream>
#include<unistd.h>
#include<sys/timerfd.h>
#include<cstdint>
//timer demo
int main()
{
    int timefd = ::timerfd_create(CLOCK_MONOTONIC,0);
    if(timefd < 0)
    {
        perror("timerfd_create error!\n");
        return -1;
    }
    //设置第一次的超时时间
    struct itimerspec settime;
    settime.it_value.tv_sec = 1;
    settime.it_value.tv_nsec = 0;
    
    //设置第一次之后的超时时间
    settime.it_interval.tv_sec = 1;
    settime.it_interval.tv_nsec = 0;
    int res = ::timerfd_settime(timefd,0,&settime,nullptr);
    if(res < 0)
    {
        perror("timerfd_settime error\n");
        return -2;
    }
    while(true)
    {
        uint64_t flag = 0;
        ssize_t n = ::read(timefd,&flag,sizeof(flag));
        if(n < 0)
        {
            perror("read error!\n");
            return -3;
        } 
        std::cout<<"超时！距离上一次超时了"<<flag<<"次"<<std::endl;
    }
    ::close(timefd);
    return 0;
}